package com.cl.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

import com.alibaba.fastjson.JSONObject;
import com.cl.model.bean.SessionUserM;
import com.cl.util.Constant;


/**
 * 
 * @author chen
 *
 */
public class BaseController {
	@Autowired  
	private HttpSession session;
	
	public final static String SUCCESS = "success";
	public final static String MSG = "msg";
	
	public static Logger logger = Logger.getLogger(BaseController.class);

	
	@InitBinder
	protected void initBinder(HttpServletRequest request, ServletRequestDataBinder binder) throws Exception {
//		binder.registerCustomEditor(Date.class,
//				new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
	}
	
	/**
	 * 所有ActionMap 统一从这里获取
	 * 
	 * @return
	 */
	public Map<String, Object> getRootMap() {
		Map<String, Object> rootMap = new HashMap<String, Object>();
		return rootMap;
	}
	

	/**
	 * 转向对应的视图，并将数据带过去
	 * @param viewName
	 * @param context
	 * @return
	 */
	public ModelAndView forward(String viewName, Map<String,Object> context) {
		return new ModelAndView(viewName, context);
	}

	/**
	 * 重定向到对应的视图
	 * @param viewName
	 * @param context
	 * @return
	 */
	public ModelAndView redirect(String viewName, Map<String,Object> context) {
		return new ModelAndView(new RedirectView(viewName), context);
	}
	
	/**
	 * 返回简单成功信息
	 * @param response
	 * @param msg
	 */
	public void sendSingleSuccessMsg(HttpServletResponse response, String msg){
		JSONObject result = new JSONObject();
		result.put(SUCCESS, true);
		result.put(MSG, msg);
		sendMsg(response, result.toJSONString());
	}
	
	/**
	 * 返回简单失败信息
	 * @param response
	 * @param msg
	 */
	public void sendSingleErrorMsg(HttpServletResponse response, String msg){
		JSONObject result = new JSONObject();
		result.put(SUCCESS, false);
		result.put(MSG, msg);
		sendMsg(response, result.toJSONString());
	}
	
	/**
	 * ajax 请求返还数据，自定义json数据
	 * @param response
	 * @param json
	 */
	public void sendMsg(HttpServletResponse response, String json){
		try {
			response.setContentType("application/json");
			response.setHeader("Pragma", "No-cache");
			response.setHeader("Cache-Control", "no-cache");
			response.setCharacterEncoding("UTF-8");
			PrintWriter out = response.getWriter();
			out.print(json);
			out.flush();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	
	public String getCurrOpenId(){
		return (String)session.getAttribute("openId");
	}
	
	public void setCurrOpenId(String openId){
		session.setAttribute("openId", openId);
	}
	
	/**
	 * 获取管理后台当前用户
	 * @return
	 */
	public SessionUserM getCurrSessionUserM(){
		return (SessionUserM)session.getAttribute(Constant.SESSION_USER_M);
	}
	
	/**
	 * 设置管理后台当前用户
	 * @param user
	 */
	public void setCurrSessionUserM(SessionUserM user){
		session.setAttribute(Constant.SESSION_USER_M, user);
	}
	
	/**
	 * 清空session
	 */
	public void cleanSession(){
		session.invalidate();
	}
	
}